<template>
  <div class="content-box">
    <div class="search-container">
      <el-input style="width: 200px" placeholder="请输入会员ID" suffix-icon="el-icon-search" v-model="id"></el-input>
      <el-input style="width: 200px" placeholder="请输入会员名字" suffix-icon="el-icon-user" class="ml-5"
        v-model="memName"></el-input>
      <el-button class="ml-5" type="primary" @click="search">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <!-- 表头 -->
    <div class="search-container">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm class="ml-5" confirm-button-text='确定' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
        title="您确定批量删除这些数据吗？" @confirm="delBatch">
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>
    <!-- 表格 -->
    <div class="table-container">
      <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
        @selection-change="handleSelectionChange" ref="table" :v-model="selection">
        <el-table-column type="selection"></el-table-column>
        <el-table-column prop="id" label="会员ID"></el-table-column>
        <el-table-column prop="memName" label="会员名字"></el-table-column>
        <el-table-column prop="memTelephone" label="会员电话"></el-table-column>
        <el-table-column prop="sex" label="性别"></el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="primary" @click="handleEdit(scope.row)" icon="el-icon-edit" circle></el-button>
            <el-popconfirm class="ml-5" confirm-button-text='确定' cancel-button-text='取消' icon="el-icon-info"
              icon-color="red" title="您确定删除吗？" @confirm="del(scope.row.id)">
              <el-button type="danger" slot="reference" icon="el-icon-delete" circle></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNumber"
        :page-sizes="[2, 5, 10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!-- 新增模态款 -->
    <el-dialog title="新增会员" :visible.sync="dialogADD" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="姓名">
          <el-input v-model="form.memName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.memTelephone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="form.sex" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogADD = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑模态款 -->
    <el-dialog title="编辑会员" :visible.sync="dialogUpdate" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="会员ID">
          <el-input v-model="form1.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form1.memName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form1.memTelephone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="form1.sex" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUpdate = false">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
  
<script>
import { getUserList, deleteUser, deleteUsers, addUser, updateUser } from "@/api/user";
export default {
  name: "User",
  data() {
    return {
      tableData: [],
      id: '',
      memName: '',
      ids: {},
      total: 0,
      pageNumber: 1,
      pageSize: 10,
      form: {},
      form1: {},
      searchForm: {
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
      },
      dialogUpdate: false,
      dialogADD: false,
      multipleSelection: [],
      selection: [], // 添加selection属性
    }
  },
  mounted() {
    this.load();
  },
  methods: {
    //初始化数据
    load() {
      this.getDataList();
    },
    //搜索
    search() {
      this.searchForm.id = this.id;
      this.searchForm.memName = this.memName;
      this.load();
      this.searchForm.id = '';
      this.searchForm.memName = '';
    },
    //重置
    reset() {
      this.id = "";
      this.memName = "";
      this.search();
    },
    //复选框选中状态
    handleSelectionChange(selection) {
      this.selection = selection;
      let arr = [];
      for (var i = 0; i < selection.length; i++) {
        arr[i] = selection[i].id;
      }
      this.ids = arr + '';
      // console.log(this.ids)
    },

    handleSizeChange(pageSize) {
      this.searchForm.pageSize = pageSize;
      this.search();
    },
    handleCurrentChange(pageNum) {
      this.searchForm.pageNumber = pageNum;
      this.search();
    },

    // 获取数据
    getDataList() {
      // this.loading = true;
      getUserList(this.searchForm).then((res) => {
        // console.log(res.result)
        if (res.success == true) {
          this.tableData = res.result.records;
          this.total = res.result.total;
        }
      });
    },
    // 新增显示模态款
    handleAdd() {
      this.dialogADD = true
      this.form = {}
    },
    // 编辑显示模态款
    handleEdit(row) {
      this.form1 = { ...row };
      this.dialogUpdate = true
    },
    //编辑用户
    update() {
      // 更新用户
      this.form1.sex = parseInt(this.form1.sex);
      updateUser(this.form1.id, this.form1).then((res) => {
        if (res.success === true) {
          this.$message.success("更新成功！");
          this.dialogUpdate = false;
          this.load();
        } else {
          this.$message.error("更新失败！");
        }
      });
    },
    //新增用户 
    add() {
      // 添加新用户
      this.form.sex = parseInt(this.form.sex);
      // console.log(this.form)
      addUser(this.form).then((res) => {
        // console.log(res);
        if (res.success === true) {
          this.$message.success("新增成功！");
          this.dialogADD = false;
          this.load();
        } else {
          this.$message.error("新增失败！");
        }
      });
    },
    // 删除单个用户
    del(id) {
      deleteUser(id).then((res) => {
        if (res.success === true) {
          this.$message.success("删除成功！");
          this.load();
        } else {
          this.$message.error("删除失败！");
        }
      });
    },
    //删除多个用户
    delBatch() {
      deleteUsers(this.ids).then((res) => {
        if (res.success == true) {
          this.$message.success("全部删除成功！");
          this.load();
        }
        else {
          this.$message.error("全部删除失败！");
        }
      })
    }
  }
}
</script>
<style>
.headerBg {
  background: #eee !important;
}

.content-box {
  width: calc(100% - 10px);
  height: 100%;
  display: flex;
  flex-direction: column;
}

.table-container {
  flex: 1;
  overflow-y: auto;
}

.el-button.is-circle {
  margin-right: 10px;
}

.search-container {
  padding: 0, 10px;
}

.el-card__body,
.el-main {
  padding: 10px 20px 20px 20px;
}

.pagination-container {
  height: 30px;
  padding-top: 10px;
}
</style>